package cn.tedu.tmall.admin.content.service.impl;

import cn.tedu.tmall.admin.content.dao.repository.IArticleRepository;
import cn.tedu.tmall.admin.content.dao.repository.ICategoryRepository;
import cn.tedu.tmall.admin.content.pojo.param.ArticleAddParam;
import cn.tedu.tmall.admin.content.pojo.po.ArticleDetailPO;
import cn.tedu.tmall.admin.content.pojo.po.ArticlePO;
import cn.tedu.tmall.admin.content.pojo.po.CategoryPO;
import cn.tedu.tmall.admin.content.pojo.vo.ArticleDetailVO;
import cn.tedu.tmall.admin.content.pojo.vo.ArticleVO;
import cn.tedu.tmall.admin.content.service.IArticleService;
import cn.tedu.tmall.common.exception.ServiceException;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务端-文章接口实现类
 */
@Slf4j
@Service
public class ArticleService implements IArticleService {
    @Autowired
    private IArticleRepository articleRepository;

    @Autowired
    private ICategoryRepository categoryRepository;

    //增加文章业务逻辑：验证登录状态、文章标题不能重复、类别是否存在、类别状态（是否禁用）
    //save保存，返回成功或异常信息
    @Override
    public void addArticle(ArticleAddParam articleAddParam) {
        log.debug("Service-增加文章-入参:{}",articleAddParam);
        //判断文章标题是否重复
        String title = articleAddParam.getTitle();
        Integer articleCount = articleRepository.countArticleByTitle(title);
        if (articleCount != 0) {//标题重复
            log.error("Service-新增文章-标题重复:{}",title);
            throw new ServiceException(ServiceCode.FAIL.getCode(), "文章标题已存在");
        }
        //判断类别是否存在
        Long categoryId = articleAddParam.getCategoryId();
        CategoryPO categoryPO = categoryRepository.getCategoryById(categoryId);
        if (categoryPO == null) {//类别不存在
            log.error("Service-新增文章-类别id不存在:{}",categoryId);
            throw new ServiceException(ServiceCode.FAIL.getCode(), "文章类别不存在");
        }
        //判断类别状态
        Integer enable = categoryRepository.getEnableById(categoryId);
        if (enable == 0) {//类别被禁用
            log.error("Service-新增文章-类别被禁用-类别id:{},enable:{}",categoryId,enable);
            throw new ServiceException(ServiceCode.FAIL.getCode(),"类别已禁用");
        }
        //通过校验，正常新增文章
        ArticlePO articlePO = new ArticlePO();
        articlePO.setTitle(articleAddParam.getTitle());
        articlePO.setCategoryId(articleAddParam.getCategoryId());
        log.debug("Service-新增文章-保存文章:{}",articlePO);
        Integer articleRows = articleRepository.saveArticle(articlePO);
        //后端异常或网络抖动，导致数据保存失败
        if (articleRows != 1) {
            log.error("Service-新增文章-服务异常，保存文章失败");
            throw new ServiceException(ServiceCode.FAIL.getCode(), "文章发布失败！");
        }
        //新增文章详情
        ArticleDetailPO articleDetailPO = new ArticleDetailPO();
        articleDetailPO.setArticleId(articlePO.getId());
        articleDetailPO.setDetail(articleAddParam.getContent());
        log.debug("Service-新增文章-保存文章详情:{}",articleDetailPO);
        //模拟后端异常或网络问题
//        "".substring(10);
        Integer articleDetailRows = articleRepository.saveArticleDetail(articleDetailPO);
        //后端异常或网络抖动，导致数据保存失败
        if (articleDetailRows != 1) {
            log.error("Service-新增文章-服务异常，保存文章详情失败");
            throw new ServiceException(ServiceCode.FAIL.getCode(), "文章发布失败！");
        }
    }

    @Override
    public PageData<ArticleVO> list(Long categoryId, Integer pageNum, Integer pageSize) {
        PageData<ArticlePO> articlePOS = articleRepository.selectList(categoryId, pageNum, pageSize);
        List<ArticleVO> articleVOS = articlePOS2VOS(articlePOS.getList());
        PageData<ArticleVO> pageData = new PageData<>();
        BeanUtils.copyProperties(articlePOS, pageData);
        pageData.setList(articleVOS);
        log.debug("Service-文章列表:{}",pageData);
        return pageData;
    }

    @Override
    public ArticleDetailVO detail(Long articleId) {
        ArticlePO basic = articleRepository.selectBasicInfo(articleId);
        log.debug("basic:{}",basic);
        if (basic == null) {
            log.error("Service-文章基本信息-文章不存在:{}",articleId);
            throw new ServiceException(ServiceCode.FAIL.getCode(), "文章不存在");
        }
        ArticleDetailPO articleDetailPO = articleRepository.selectDetail(articleId);
        log.debug("detail:{}",articleDetailPO);
        if (articleDetailPO == null) {
            log.error("Service-文章详情-文章不存在:{}",articleId);
            throw new ServiceException(ServiceCode.FAIL.getCode(), "文章不存在");
        }
        ArticleDetailVO articleDetailVO = new ArticleDetailVO();
        BeanUtils.copyProperties(basic,articleDetailVO);
        BeanUtils.copyProperties(articleDetailPO,articleDetailVO);
        log.debug("Service-文章详情:{}",articleDetailVO);
        return articleDetailVO;
    }

    private ArticleVO articlePO2VO(ArticlePO po){
        ArticleVO vo = new ArticleVO();
        BeanUtils.copyProperties(po,vo);
        return vo;
    }

    private List<ArticleVO> articlePOS2VOS(List<ArticlePO> pos){
        List<ArticleVO> vos = new ArrayList<>();
        for (ArticlePO po : pos) {
            vos.add(articlePO2VO(po));
        }
        return vos;
    }
}
